import type { App } from 'vue'
import type { RouteRecordRaw } from 'vue-router'

import { createRouter, createWebHistory } from 'vue-router'

const Layout = () => import('@/layout/layout.vue')

export const constantRouterMap: AppRouteRecordRaw[] = [
    {
        children: [
            {
                component: () => import('@/pages/member/index.vue'),
                meta: {
                    title: '严选会员',
                },
                name: 'member',
                path: 'member',
            },
            {
                component: () => import('@/pages/member/components/add.vue'),
                meta: {
                    activeMenu: '/member',
                    title: '开通会员',
                },
                name: 'member-add',
                path: 'member-add',
            },
            {
                component: () => import('@/pages/invoice/index.vue'),
                meta: {
                    title: '发票管理',
                },
                name: 'invoice',
                path: 'invoice',
            },
        ],
        component: Layout,
        meta: {
            hidden: true,
            noTagsView: true,
            title: '首页',
        },
        name: 'root',
        path: '/',
        redirect: '/member',
    },
    {
        children: [
            {
                component: () => import('@fl/views/redirect/redirect.vue'),
                meta: {},
                name: 'Redirect',
                path: '/redirect/:path(.*)',
            },
            {
                component: () => import('@fl/views/error/404.vue'),
                meta: {
                    hidden: true,
                    noTagsView: true,
                    title: '404',
                },
                name: 'NoFind',
                path: '/404',
            },
        ],
        component: Layout,
        meta: {
            hidden: true,
            noTagsView: true,
        },
        name: 'Redirect',
        path: '/redirect',
    },
]

export const asyncRouterMap: AppRouteRecordRaw[] = [
    {
        children: [
            {
                meta: {
                    title: '严选会员',
                },
                name: 'member',
                path: 'member',
            },
            {
                meta: {
                    hidden: true,
                    title: '开通会员',
                },
                name: 'member-add',
                path: 'member-add',
            },
            {
                meta: {
                    title: '发票管理',
                },
                name: 'invoice',
                path: 'invoice',
            },
        ],
        component: Layout,
        meta: {
            alwaysShow: true,
            title: '会员管理',
        },
        name: '',
        path: '',
    },
]

const router = createRouter({
    history: createWebHistory(),
    routes: constantRouterMap as RouteRecordRaw[],
    scrollBehavior: () => ({ left: 0, top: 0 }),
    strict: true,
})

// export function resetRouter(): void {
//     const resetWhiteNameList = ['Redirect', 'Login', 'NoFind', 'Root']
//     router.getRoutes().forEach((route) => {
//         const name: any = route.name
//         if (name && !resetWhiteNameList.includes(name))
//             router.hasRoute(name) && router.removeRoute(name)
//     })
// }

export function setupRouter(app: App<Element>) {
    app.use(router)
}

export default router
